﻿package game.music {
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.Event;
	import flash.media.SoundTransform;
	import game.ui.Button;
	import flash.events.MouseEvent;
	
	public class Music {
		
		private var soundMain:Sound;
		private var channelMain:SoundChannel;
		private var soundTransform:SoundTransform;
		private var btnOn:Button;
		private var btnOff:Button;
		
		public function Music():void {
			
			soundTransform = new SoundTransform (1);
			
		}
		
		private function playSound ( sound:Sound = null, start:Number = 0, loops:int = 0, volume:Number = -1 ):SoundChannel {
			
			var channel:SoundChannel;
			if (!sound) {
				sound = soundMain;
				channel = channelMain;
			}
			
			if (volume!=-1) {
				soundTransform.volume = volume;
			}
			
			channel = sound.play ( start, loops, soundTransform );
			channel.addEventListener ( Event.SOUND_COMPLETE, channelComplete );
			
			return channel;
		}
		
		private function channelComplete(event:Event):void {
			trace ('channelComplete');
			//next ();
		}
		
		public function set buttonOn(value:Button):void {
			btnOn = value;
			btnOn.addEventListener(MouseEvent.CLICK, onHandler);
			if (soundTransform.volume>0)
				btnOn.visible = false;
		}
		
		public function set buttonOff(value:Button):void {
			btnOff = value;
			btnOff.addEventListener(MouseEvent.CLICK, offHandler);
			if (soundTransform.volume<=0)
				btnOff.visible = false;
		}
		
		private function onHandler(event:MouseEvent):void {
			soundTransform.volume = 1;
			if (btnOn)
				btnOn.visible = false;
			if (btnOff)
				btnOff.visible = true;
			if (channelMain)
				channelMain.soundTransform = soundTransform;
		}
		
		private function offHandler(event:MouseEvent):void {
			soundTransform.volume = 0;
			if (btnOff)
				btnOff.visible = false;
			if (btnOn)
				btnOn.visible = true;
			if (channelMain)
				channelMain.soundTransform = soundTransform;
		}
		
	}
}


/*package  {
	
	import flash.display.MovieClip;
	import flash.system.Security;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.ProgressEvent;
	import flash.events.MouseEvent;
	import flash.media.SoundTransform;
	import flash.net.SharedObject;
	
	public class mp3 extends MovieClip {
		
		private var count:int = 0;
		private var current:int = 0;
		private var tracks:Array = new Array;
		private var sound:Sound;
		private var channel:SoundChannel;
		private var timer:Timer;
		private var _volume:Number = 1;
		public var playing:Boolean = false;
		private var so:SharedObject = SharedObject.getLocal('player');
			
		public function mp3() {
			Security.allowDomain("*");
			
			artist.text	= "";
			title.text	= "";
			
			this.alpha = 0;
			this.addEventListener(Event.ADDED_TO_STAGE, added);
		}
		
		private function added(event:Event):void {
			
			this.alpha = 0;
			stage.addEventListener(Event.RESIZE, resized);
			
			soundScroll.mouseChildren = soundScroll.mouseEnabled = false;
			soundMask.mouseChildren = soundMask.mouseEnabled = false;
			scrollBar.mouseChildren = scrollBar.mouseEnabled = false;
			scrollMask.mouseChildren = scrollMask.mouseEnabled = false;
			btn.mouseChildren = btn.mouseEnabled = false;
			
			soundBack.buttonMode = true;
			soundBack.addEventListener(MouseEvent.MOUSE_DOWN, volumeDownHandler);
			scrollBack.addEventListener(MouseEvent.MOUSE_DOWN, seekHandler);
			
			btnRight.addEventListener(MouseEvent.CLICK, rightHandler);
			btnLeft.addEventListener(MouseEvent.CLICK, leftHandler);
			
			btnPause.visible = false;
			btnPlay.visible = true;
			
			btnPause.addEventListener(MouseEvent.CLICK, pauseHandler);
			btnPlay.addEventListener(MouseEvent.CLICK, playHandler);
			btnPause.useHandCursor = false;
			btnPlay.useHandCursor = false;
			btnRight.useHandCursor = false;
			btnLeft.useHandCursor = false;
			
			if (so.data.vol) _volume = so.data.vol;
			
			
			var test:Object = new Object;
			test[0] = 2;
			test[1] = {
				aid: 175707019,
				owner_id: -44887765,
				artist: 'N.E.R.D.',
				title: 'I Wanna Jam',
				duration: 214,
				url: 'http://cs4838.vk.me/u3071028/audios/9dd1b4da9b73.mp3'
			}
			test[2] = {
				aid: 174951159,
				owner_id: -44887765,
				artist: 'The White Stripes',
				title: 'Seven Nation Army',
				duration: 231,
				url: 'http://cs4508.vk.me/u5503766/audios/0c4713548db3.mp3',
				lyrics_id: '45897950'
			}
			
			audio(test);
			
			
		}
		
		private function pauseHandler(event:MouseEvent):void {
			playing = false;
			
			channel.stop();
			
			updatePlayButtons();
		}
		
		private function playHandler(event:MouseEvent):void {
			playing = true;
			
			playAt (channel.position);
			
			updatePlayButtons();
		}
		
		private function updatePlayButtons():void {
			if (playing) {
				btnPause.visible = true;
				btnPlay.visible = false;
			} else {
				btnPause.visible = false;
				btnPlay.visible = true;
			}
		}
		
		private function rightHandler(event:MouseEvent):void {
			next();
		}
		
		private function leftHandler(event:MouseEvent):void {
			prev();
		}
		
		private function seekHandler(event:MouseEvent):void {
			var X:Number = (scrollBack as MovieClip).mouseX;
			if (X<0) X = 0;
			if (X>(scrollBack as MovieClip).width) X = (scrollBack as MovieClip).width;
			var percent:Number = X/(scrollBack as MovieClip).width*duration;
			channel.stop();
			
			playAt(percent*1000);
			
		}
		
		private function volumeDownHandler(event:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, volumeUpHandler);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, volumeMoveHandler);
			
			volumeMoveHandler(null);
		}
		
		private function volumeMoveHandler(event:MouseEvent):void {
			var X:Number = (soundBack as MovieClip).mouseX;
			if (X<3) X = 0;
			if (X>(soundBack as MovieClip).width) X = (soundBack as MovieClip).width;
			var percent:Number = X/(soundBack as MovieClip).width;
			
			volume = percent;
		}
		
		private function volumeUpHandler(event:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, volumeUpHandler);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, volumeMoveHandler);
			
			so.data.vol = volume;
		}
		
		private function soundProgress(event:ProgressEvent):void {
			var percent:Number = event.bytesLoaded/event.bytesTotal;
			progress(percent);
		}
		
		private function soundComplete(event:Event):void {
			trace ('soundComplete');
		}
		
		private function soundError(event:IOErrorEvent):void {
			trace ('soundError');
		}
		
		private function channelComplete(event:Event):void {
			trace ('channelComplete');
			next ();
		}
		
		private function timeHandler(event:TimerEvent):void {
			var percent:Number = channel.position/(duration*1000);
			
			time.text = getTime(duration - Math.floor(channel.position/1000));
			btn.x = scrollMask.x + scrollMask.width*percent;
		}
		
		private function getTime(value:Number):String {
			var min:Number = Math.floor(value/60);
			var sec:Number = value - min*60;
			return ((min<10) ? "0"+String(min) : String(min)) + ":" + ((sec<10) ? "0"+String(sec) : String(sec));
		}
		
		public function audio(object:Object):void {
			count = object[0];
			for (var i:int = 1; i<=count; i++) {
				tracks.push(object[i]);
			}
			
			current = Math.round(Math.random()*(tracks.length-1));
			
			if (count>0)
				playNext();
		}
		
		public function next():void {
			current ++;
			if (current==count)
				current = 0;
			playNext();
		}
		
		public function prev():void {
			current --;
			if (current<0) 
				current = count - 1;
			playNext();
		}
		
		public function playNext():void {
			close();
			
			artist.text = track.artist;
			title.text = track.title;
			counter.text = String(current+1)+'/'+String(count);
			
			progress(0);
			
			sound = new Sound;
			sound.addEventListener(Event.COMPLETE, soundComplete);
			sound.addEventListener(IOErrorEvent.IO_ERROR, soundError);
			sound.addEventListener(ProgressEvent.PROGRESS, soundProgress);
			
			var req:URLRequest = new URLRequest ( track.url );
			sound.load( req );
			
			playAt(0);
			
			timer = new Timer(50);
			timer.addEventListener(TimerEvent.TIMER, timeHandler);
			timer.start();
			
			volume = _volume;
			
			playing = true;
			
			updatePlayButtons();
			
		}
		
		private function playAt(value:Number) {
			
			channel = sound.play(value, 0, new SoundTransform(_volume));
			channel.addEventListener(Event.SOUND_COMPLETE, channelComplete);
			
		}
		
		private function progress(value:Number):void {
			scrollBar.x = scrollMask.x - scrollMask.width + scrollMask.width*value;
		}
		
		public function get track():Object {
			return tracks[current];
		}
		
		public function get duration():Number {
			return track.duration;
		}
		
		public function set volume(value:Number):void {
			if (channel) {
				var transform:SoundTransform = channel.soundTransform;
				transform.volume = value;
				channel.soundTransform = transform;
			}
			
			var a:Number = value*0.4;
			
			this.alpha = 0.5+a;
			
			_volume = value;
				
			soundScroll.x = soundMask.x - soundMask.width + soundMask.width*value;
		}
		
		public function get volume():Number {
			return _volume;
		}
		
		public function close():void {
			try {
				channel.stop();
				sound.close();
			} catch (e:Error) {
				trace (e.message);
			}
			if (timer) {
				timer.stop();
				timer = null;
			}
			
			playing = false;
			
			updatePlayButtons();
			
		}
		
		private function resized(event:Event):void {
			
		}
	}
	
}
*/